package com.qinyh.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.qinyh.entity.Book;
import com.qinyh.entity.Chapter;
import com.qinyh.util.TemplateStore;

public class MenuActivity extends Activity {

	private Book currentBook;
	private ListView lv;
	MenuInflater mi;
	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.book_menu);
		lv = (ListView) findViewById(R.id.book_menu_list);
		currentBook = TemplateStore.getCurrentBook();
		if (currentBook != null) {
			setAdapter();
		}
		mi = new MenuInflater(this);
	}	

	private void setAdapter() {
		Log.v("debug", "apper chapter when click the book on the shelf!");
		List<String> lstring = new ArrayList<String>();
		for (String s : currentBook.getChapterTitles()) {
			lstring.add(s);
		};
		ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, lstring);
		lv.setAdapter(aa);
		lv.setOnItemClickListener(new ListView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int seq,
					long aseq) {
				try {
					Chapter chapter = new Chapter();
					chapter.setChapterName(currentBook.getChapterTitles().get(
							seq));
					/*chapter.setChapterContent(currentBook.getChapterContents()
							.get(seq));*/
					//设置存储模板当前正在看的章节 和章节对应的序号
					TemplateStore.setCurrentChapterSeq(seq);
					TemplateStore.setCurrentChapter(chapter);
					TemplateStore.setCurrentPage(null);
					TemplateStore.setNextPage(null);
					TemplateStore.setPrePage(null);
					TemplateStore.setCurrentStart(0);
					TemplateStore.setCurrentEnd(0);
					Intent intent = new Intent(MenuActivity.this,
							PageActivity.class);
					startActivity(intent);
				} catch (Exception exp) {
					Toast.makeText(MenuActivity.this,
							exp.getStackTrace()[0] + exp.getMessage(),
							Toast.LENGTH_SHORT).show();
				}
			}
		});
	}
	public boolean onCreateOptionsMenu(Menu menu) {
		mi.inflate(R.menu.txtmenu, menu);
		return true;
		
	}
}
